package com.jrh.web.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jrh.dal.ApplyDao;
import com.jrh.dal.LabelDao;
import com.jrh.domain.common.Page;
import com.jrh.domain.entity.Apply;
import com.jrh.domain.entity.Label;

/**
 * 后台申请管理
 * 
 * @author yout 2012-8-12 下午2:17:22
 */
@Controller
@RequestMapping("/admin/apply")
public class AdminApplyController {

    private static final Integer pageSize = 5;

    @Autowired
    private ApplyDao             applyDao;

    @Autowired
    private LabelDao             labelDao;

    @RequestMapping("/")
    public String view(Integer pageIndex, Model model) {
        if (pageIndex == null) {
            pageIndex = 1;
        }
        Integer start = (pageIndex - 1) * pageSize;
        Page<Apply> list = applyDao.list(null, start, pageSize);
        model.addAttribute("list", list);
        return "admin/apply/list";
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String updateView(@PathVariable("id") long id, Model model, String success) {
        Apply apply = applyDao.load(id);
        model.addAttribute("apply", apply);
        List<Label> styles = labelDao.listChildLabel(Label.LABEL_STYLE_KEY);
        model.addAttribute("styles", styles);

        model.addAttribute("success", Boolean.valueOf(success));
        return "admin/apply/update";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Apply apply, Model model) {
        applyDao.update(apply);
        return "redirect:/admin/apply/update/" + apply.getId() + "?success=" + Boolean.TRUE.toString();
    }

}
